package com.cellulam.framework.datasource;

import com.cellulam.framework.core.annotations.TransTargetDataSource;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.stereotype.Component;

@Aspect
@EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true)
@Component
public class DataSourceIntercept implements PriorityOrdered {
    @Before("@annotation(targetDataSource)")
    public void switchDataSource(TransTargetDataSource targetDataSource) {
        DataSourceContextHolder.switchDataSource(targetDataSource.value());
    }

    /**
     * 值越小，越优先执行
     * 要优于事务的执行
     * 在启动类中加上了@EnableTransactionManagement(order > Intercept order)
     */
    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE + 10;
    }
}
